<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>菜鸟教程(runoob.com)</title>

        <script type="text/javascript">
            function WebSocketTest() {
                if ("WebSocket" in window) {
                    console.log("您的浏览器支持 WebSocket!");

                    // 打开一个 web socket
                    var ws = new WebSocket("ws://localhost:8888/websocket");

                    ws.onopen = function() {
                        // Web Socket 已连接上，使用 send() 方法发送数据
                        // ws.send("发送数据");
                        var a = {
                            HeadProto: {
                                AreaCode: 1,
                                OpCode: 1
                            },

                            Code: "123",
                            UserName: "leiguan",
                            UserPW: "zaodian",
                            Email: "crylg@qq.com"
                        };

                        ws.send(JSON.stringify(a));
                        console.log("数据发送中...");
                    };

                    ws.onmessage = function(evt) {
                        var received_msg = evt.data;
                        console.log("数据已接收...");
                    };

                    ws.onclose = function() {
                        // 关闭 websocket
                        console.log("连接已关闭...");
                    };
                } else {
                    // 浏览器不支持 WebSocket
                    console.log("您的浏览器不支持 WebSocket!");
                }
            }
        </script>
    </head>

    <body>
        <div id="sse">
            <a href="javascript:WebSocketTest()">运行 WebSocket</a>
        </div>
    </body>
</html>
